package com.tedu.udm.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.udm.pojo.dto.PermissionAddNewDTO;
import com.tedu.udm.pojo.vo.PermissionListItemVO;
import com.tedu.udm.service.IPermissionService;
import com.tedu.udm.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "03. 权限管理模块")
@RestController
@RequestMapping("/permissions")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    public PermissionController() {
        log.info("创建控制器对象：PermissionController");
    }

    // http://localhost:9878/roles/add-new?username=aa&phone=bb&email=cc
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(PermissionAddNewDTO permissionAddNewDTO) {
        log.debug("开始处理【添加权限】的请求，参数：{}", permissionAddNewDTO);
        permissionService.addNew(permissionAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/roles/add-new?username=aa&phone=bb&email=cc
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 100)
    @PostMapping("/{id:[0-9]+}/add-new")
    public JsonResult<Void> addNewWithRole(@Range(min = 1, message = "删除权限失败，尝试删除的权限的ID无效！")
                                               @PathVariable Long roleId,PermissionAddNewDTO permissionAddNewDTO) {
        log.debug("开始处理【添加权限】的请求，参数：{}", permissionAddNewDTO);
        permissionService.addNewWithRole(roleId,permissionAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/permissions/9527/delete
    @ApiOperation("根据id删除权限")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "权限id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除权限失败，尝试删除的权限的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除权限】的请求，参数：{}", id);
        permissionService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9878/roles
    @ApiOperation("查询权限列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<PermissionListItemVO>> list() {
        log.debug("开始处理【查询权限列表】的请求，无参数");
        List<PermissionListItemVO> list = permissionService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9878/roles
    @ApiOperation("查询角色权限列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParam(name = "id", value = "权限id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/list")
    public JsonResult<List<PermissionListItemVO>> listByRoleId(@Range(min = 1, message = "删除权限失败，尝试删除的权限的ID无效！")
                                                                   @PathVariable Long id) {
        log.debug("开始处理【查询角色权限列表】的请求，无参数");
        List<PermissionListItemVO> list = permissionService.listByRoleId(id);
        return JsonResult.ok(list);
    }
}
